import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))

const nodeTypeData = [{ name:'开始', value:'1' },{ name:'普通', value:'3' },{ name:'结束', value:'2' } ]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
			forms : [
				{ label: '节点名称',		name: 'node_name' },
				{ label: '节点代码',		name: 'node_code' },
				{ label: '节点类型',		name: 'node_type',	type: 'select', data:nodeTypeData}, 
				{ label: '流程名称',		name: 'process_id',	type: 'select', loading:true },
			],
		// 提交表单列表
			formData : [
				{ label: '节点类型',		name: 'node_type', 		half:true,  required:true,type: 'select', data:nodeTypeData },
				{ label: '节点名称',		name: 'node_name', 		half:true, 	required:true,},
				{ label: '所属流程',		name: 'process_id', 	half:true, 	required:true,type: 'select'},
				{ label: '节点排序',		name: 'sort', 			half:true, 	required:true },
			]
	}
	
	model = {}
	
	componentDidMount(){
		this.uuid = $fn.query('uuid')
		if(this.uuid){ this.query = { process_id: this.uuid } }
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => { 
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 流程名称
			$fn.setRows( [forms, formData], 'process_id', await f.getCache({ api:'process_name',refresh:true })  )
			//
			if(this.uuid){
				this.searchFormRef.setValue({ process_id: this.uuid  })
				this.formRef.setValue({ process_id: this.uuid  })
			}
			// 
			this.setState({ forms })
		})
		
	}
	// paging
	fetch = () => $fn.fetch.call(this,'fl-process-node/index')
	// 表单
	cols = [
		{ title: '节点代码', 	dataIndex: 'node_code', 		width:150 },
		{ title: '节点名称', 	dataIndex: 'node_name', 		width:200 },
		{ title: '所属流程', 	dataIndex: 'process_name', 		width:200 },
		{ title: '排序', 		dataIndex: 'sort', 				...$fn.table.num },
		{ title: '节点类型', 	dataIndex: 'node_type', 		width:100, align:'center', render: text => $fn.getState(nodeTypeData,+text)  },
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		TableControls.controls.call(this, { width: 200 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'fl-process-node/info', { uuid }),					// 编辑
			this.onDel([ uuid ]),																	// 删除
			TableControls.pushTo.call(this, '查看授权',  `process-permission-list?uuid=${uuid}` )
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this),				// 添加
		this.onDel(),							// 删除
		this.uuid && Controls.back.call(this)	// 返回
	]
	
	// 删除
	onDel = uuid => Delete.call(this, 'fl-process-node/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='节点列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'fl-process-node/add', editApi:'fl-process-node/edit' }) }
			</>
		)
	}
}

export default Index